package com.reader_;

import org.junit.Test;

import java.io.FileReader;
import java.io.IOException;

/**
 * @author Vhong
 * @version 1.0
 */
public class FileReader_ {
    public static void main(String[] args) {

    }
    /*
        单个字符读取文件
     */
    @Test
    public void readFile01(){
        String filePath = "e:\\story.txt";
        FileReader fileReader = null;
        int data = 0;

        try {
            fileReader = new FileReader(filePath);
            while((data = fileReader.read()) != -1){
                System.out.print((char)data);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(fileReader != null){
                try {
                    fileReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    /*
     字符数组读取文件
     */
    @Test
    public void readFile02(){
        String filePath = "e:\\story.txt";
        FileReader fileReader = null;
        int readLen = 0;
        char[] buf = new char[8];
        //1.创建FileReader对象


        try {
            fileReader = new FileReader(filePath);
            //循环读取 使用read（buf），返回的是实际读取到的字符数
            //如果返回-1，说明到文件结束
            while(( readLen = fileReader.read(buf)) != -1){
                System.out.print(new String(buf,0,readLen));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(fileReader != null){
                try {
                    fileReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
